﻿var app = angular.module('WebApp', ['ipCookie']);

app.filter("JSONDate", function ($filter) {
    return function (input, format) {
        if (input) {
            var r = input.replace(/[^0-9|-]+/g, '');
            var timestamp = Number(r);
            return $filter("date")(timestamp, format);
        }
        else {
            return input;
        }
    }
});
app.value('option', {
    date: {
        format: 'L',
        toolbarPlacement: 'bottom',
        showTodayButton: true,
        showClear: true,
        showClose: true,
        ignoreReadonly: true,
        locale: 'zh-cn'
    },
    datetime: {
        format: 'L HH:mm:ss',
        sideBySide: true,
        toolbarPlacement: 'bottom',
        showTodayButton: true,
        showClear: true,
        showClose: true,
        ignoreReadonly: true,
        locale: 'zh-cn'
    }
});
/* 自定义缓存服务 **/
app.factory('cache', function ($cacheFactory) {

    var service = {};
    var cache = $cacheFactory('myCache');

    service.get = function (key) {
        return cache.get(key);
    }

    service.put = function (key, value) {
        cache.put(key, value);
    }

    return service;
});
app.factory('common', function ($window) {
    var service = {};

    service.error = function ($scope) {
        $scope.result.code = -1;
        $scope.result.message = "服务器访问失败，请重新刷新界面";
    }

    service.success = function ($scope, returnUrl, ret) {
        if (ret.Code == 100 && returnUrl != null) {
            $window.location.href = "../.." + returnUrl;
        }
        $scope.result.code = ret.Code;
        $scope.result.message = ret.Message;
        if (ret.Data && ret.Data.CurrentPageIndex) {
            $scope.result.currentPage = ret.Data.CurrentPageIndex;
            $scope.result.pageSize = ret.Data.PageSize;
            $scope.result.totalPage = ret.Data.TotalPageCount;
            $scope.result.pages = ret.Data.Pages;
            $scope.result.totalItemCount = ret.Data.TotalItemCount;
        }
    }

    service.addUrlParamVal = function (url, key, value) {
        if (/\?/g.test(url)) {
            var pattern = new RegExp(key + '=([^&]*)');
            if (url.match(pattern)) {
                var tmp = '/(' + key + '=)([^&]*)/gi';
                url = url.replace(eval(tmp), key + "=" + value);
            } else {
                url += "&" + key + "=" + value;
            }
        }
        else {
            url += "?" + key + "=" + value;
        }

        return url;
    }

    service.removeUrlParamVal = function (url, key) {
        if (/\?/g.test(url)) {
            var pattern = new RegExp(key + '=([^&]*)');
            if (url.match(pattern)) {
                var tmp = '/(&?' + key + '=)([^&]*)/gi';
                url = url.replace(eval(tmp), "");
            }
        }

        return url;
    }

    return service;
});

app.controller('MainController', function ($scope, $http, $window, cache) {
    if (true) {
        $http({
            method: 'POST',
            url: '../Admin/GetAccountInfo'
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    $scope.User = ret.Data;
                }
                else {
                    //alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
            });
    }

    if (!cache.get("routes")) {
        $http.get("../Assets/js/nav-route.json").success(function (json) {
            cache.put("routes", json);
            $scope.init();
        }).error(function (data, status, headers, config) { });
    } else {
        $scope.init();
    }

    $scope.init = function () {
        var routes = cache.get("routes");
        $scope.firstLevel = {}
        $scope.secondLevel = {}

        var path = $window.location.pathname;
        if (path == "/") path = "/GeneratorSet/GeneratorSetList";
        if (path == "/Admin") path = "/Admin/Index";
        var contrast = routes[path];
        $scope.firstLevel[contrast["first"]] = true;
        $scope.secondLevel[contrast["second"]] = true;
    };

    $scope.doLogout = function () {
        //var valid = confirm("确认退出?");
        //if (valid) {
        //    $http.post('../Account/Logout')
        //        .success(function (ret) {
        //            document.cookie == "";
        //            $window.location.href = $window.location.href;
        //        })
        //        .error(function (data, status, headers, config) {

        //        });
        //}

        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认退出当前账号？", function (ev) {
                ev.preventDefault();
                $http.post('../Account/Logout')
                    .success(function (ret) {
                        document.cookie == "";
                        $window.location.href = $window.location.href;
                    })
                    .error(function (data, status, headers, config) {

                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }





});
